//
//  WXVideoHomeCollectionReusableView.m
//  NewGuiYang
//
//  Created by 罗江 on 2018/5/25.
//  Copyright © 2018年 luojiang. All rights reserved.
//

#import "WXVideoHomeCollectionView.h"
#import "WXVideoTitleCollectionReusableView.h"
#import "WXLiveVideoCollectionViewCell.h"
#import "WXVideoPlayBackCollectionViewCell.h"

#import <WXCloudModuleBaseConfig/WXSystemBaseDefine.h>

@interface WXVideoHomeCollectionView () <UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UIView *lineView;

@end
@implementation WXVideoHomeCollectionView
static NSString *strCellIdentify = @"WXLiveVideoCollectionViewCell";
static NSString *strHeaderViewIdentify = @"WXVideoTitleCollectionReusableView";
static NSString *strPlayBackCellIdentify = @"WXVideoPlayBackCollectionViewCell";
#pragma mark - life cycle

-(instancetype)initWithFrame:(CGRect)frame
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WXScreenWidth/4, 110);
    flowLayout.headerReferenceSize = CGSizeMake(150, 50);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionHeadersPinToVisibleBounds = YES;// 当前组如果还在可视范围时让头部视图停留
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self = [super initWithFrame:frame collectionViewLayout:flowLayout];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.scrollEnabled = YES;
        self.delegate = self;
        self.dataSource = self;
        [self registerClass:[WXLiveVideoCollectionViewCell class] forCellWithReuseIdentifier:strCellIdentify];
        [self registerClass:[WXVideoTitleCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:strHeaderViewIdentify];
          [self registerClass:[WXVideoPlayBackCollectionViewCell class] forCellWithReuseIdentifier:strPlayBackCellIdentify];

    }
    return self;
}


#pragma mark -UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section==0) {
        return _videoHomeList.count;
    }
    else
        return _videoPlayBackList.count;
    
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        
        WXVideoTitleCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:strHeaderViewIdentify forIndexPath:indexPath];
        //headerView.titleLabel.text = @"正在直播";
        if (indexPath.section==0) {
            headerView.titleLabel.text = @"正在直播";
        }
        else{
            headerView.titleLabel.text = @"节目回放";
            [headerView addSubview:self.lineView];
        }
        return headerView;
    }
    return nil;
}
- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return  CGSizeMake(WXScreenWidth/4, 90);
    }
    else
        return  CGSizeMake(WXScreenWidth/2, 150);;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        WXLiveVideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:strCellIdentify forIndexPath:indexPath];
        WXBaseInteractiveModel *model=[_videoHomeList objectAtIndex:indexPath.row];
        [cell setModel:model];
        return cell;
    }
    else{
        WXVideoPlayBackCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:strPlayBackCellIdentify forIndexPath:indexPath];
        WXPlayBackVideoModel *model=[_videoPlayBackList objectAtIndex:indexPath.row];
        [cell setModel:model];
        return cell;
    }
}

#pragma mark -UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    WXLiveVideoCollectionViewCell *cell = (WXLiveVideoCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    [_liveVideoSelectSubject sendNext:cell.model];
}

-(UIView *)lineView{
    if (_lineView==nil) {
        _lineView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, WXScreenWidth, 6)];
        _lineView.backgroundColor=RGB(245, 245, 245);
    }
    return _lineView;
    
}

@end
